package com.bruceliu.controller;

import com.bruceliu.bean.Desk;
import com.bruceliu.bean.Food;
import com.bruceliu.bean.FoodType;
import com.bruceliu.service.DeskService;
import com.bruceliu.service.FoodService;
import com.bruceliu.service.FoodTypeService;
import com.bruceliu.utils.PageUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: RestaurantSystem
 * @BelongsPackage: com.bruceliu.controller
 * @Author: bruceliu
 * @QQ:1241488705
 * @CreateTime: 2020-03-01 14:32
 * @Description: TODO
 */
@Controller
@Scope("prototype")
public class MenuController {

    @Resource
    DeskService deskService;

    @Resource
    FoodService foodService;
    
    @Resource
    FoodTypeService foodTypeService;

    @RequestMapping("/menu_ui/{desk_id}/{pageIndex}")
    public String caidanUI(@PathVariable("pageIndex") Integer pageIndex,Integer type_id,@PathVariable("desk_id") int desk_id, HttpSession session,Model model){
        if(type_id==null){
            type_id=0;
        }
        Desk desk = deskService.getById(desk_id);
        session.setAttribute("desk",desk);

        //所有的菜系信息
        List<FoodType> foodTypes = foodTypeService.getAllFoodTypes();
        model.addAttribute("foodTypes",foodTypes);

        Map<String,Object> map=new HashMap<>();
        map.put("type_id",type_id);
        map.put("pageStart",(pageIndex-1)*6);
        map.put("pageSize",6);

        int totalCount = foodService.menu_totalCount(map);
        List<Food> foods = foodService.menu_getFoods(map);

        PageUtils pageUtils=new PageUtils(6,pageIndex,totalCount,foods);
        model.addAttribute("pageUtils",pageUtils);
        model.addAttribute("type_id",type_id); //用选择的菜系

        System.out.println(pageUtils);

        //查询页面所需要的数据
        return "caidan";
    }

    /**
     * 02-跳转到菜单详情
     * @param id
     * @return
     */
    @RequestMapping("/menu_todetail/{id}")
    public String menu_todetail(@PathVariable("id") int id,Model model){
        Food food = foodService.getById(id);
        model.addAttribute("food",food);
        //所有的菜系信息
        List<FoodType> foodTypes = foodTypeService.getAllFoodTypes();
        model.addAttribute("foodTypes",foodTypes);
        return "caixiangxi";
    }
}
